import json
from django.forms.models import model_to_dict
from django.shortcuts import render, HttpResponse

from .models import *

"""
村民
"""


def villager(request):
    """
    村民模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Villager.objects.filter().all())]
        return render(request, 'villager.html', {"data_list": data_list})


def get_villager_by_id(request):
    """
    通过id获取村民数据
    :param request:
    :return:
    """
    print(request.POST.get("id"))
    villager_data = model_to_dict(Villager.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(villager_data))


def create_villager(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Villager.objects.create(**data)

    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_villager(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Villager.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_villager(request):
    """
    删除村民数据
    :param request:
    :return:
    """

    Villager.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
党员
"""


def party_member(request):
    """
    党员模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(PartyMember.objects.filter().all())]
        return render(request, 'partyMember.html', {"party_member_data_list": data_list})


def get_party_member_by_id(request):
    """
    通过id获取党员数据
    :param request:
    :return:
    """
    party_member_data = model_to_dict(PartyMember.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(party_member_data))


def create_party_member(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        PartyMember.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_party_member(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        PartyMember.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_party_member(request):
    """
    删除党员数据
    :param request:
    :return:
    """

    PartyMember.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
妇女
"""


def women(request):
    """
    妇女模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Women.objects.filter().all())]
        return render(request, 'women.html', {"women_data_list": data_list})


def get_women_by_id(request):
    """
    通过id获取妇女数据
    :param request:
    :return:
    """
    data = model_to_dict(Women.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_women(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Women.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_women(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Women.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_women(request):
    """
    删除妇女数据
    :param request:
    :return:
    """

    Women.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
工程管理
"""


def project(request):
    """
    工程管理模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Project.objects.filter().all())]
        return render(request, 'project.html', {"data_list": data_list})


def get_project_by_id(request):
    """
    通过id获取党员数据
    :param request:
    :return:
    """
    data = model_to_dict(Project.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_project(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Project.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_project(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Project.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_project(request):
    """
    删除工程数据
    :param request:
    :return:
    """

    Project.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
资产
"""


def asset(request):
    """
    资产模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Asset.objects.filter().all())]
        return render(request, 'asset.html', {"data_list": data_list})


def get_asset_by_id(request):
    """
    通过id获取资产数据
    :param request:
    :return:
    """
    data = model_to_dict(Asset.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_asset(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Asset.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_asset(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Asset.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_asset(request):
    """
    删除资产数据
    :param request:
    :return:
    """

    Asset.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
宗教
"""


def religion(request):
    """
    宗教模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Religion.objects.filter().all())]
        return render(request, 'religion.html', {"data_list": data_list})


def get_religion_by_id(request):
    """
    通过id获取宗教数据
    :param request:
    :return:
    """
    data = model_to_dict(Religion.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_religion(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Religion.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_religion(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Religion.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_religion(request):
    """
    删除宗教数据
    :param request:
    :return:
    """

    Religion.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
企业
"""


def enterprise(request):
    """
    企业模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Enterprise.objects.filter().all())]
        return render(request, 'enterprise.html', {"data_list": data_list})


def get_enterprise_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Enterprise.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_enterprise(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Enterprise.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_enterprise(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Enterprise.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_enterprise(request):
    """
    删除数据
    :param request:
    :return:
    """

    Enterprise.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
九小
"""


def nine(request):
    """
    九小模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Nine.objects.filter().all())]
        return render(request, 'nine.html', {"data_list": data_list})


def get_nine_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Nine.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_nine(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Nine.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_nine(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Nine.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_nine(request):
    """
    删除数据
    :param request:
    :return:
    """

    Nine.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
新市民
"""


def new_citizen(request):
    """
    新市民模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(NewCitizen.objects.filter().all())]
        return render(request, 'newCitizen.html', {"data_list": data_list})


def get_new_citizen_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(NewCitizen.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_new_citizen(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        NewCitizen.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_new_citizen(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        NewCitizen.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_new_citizen(request):
    """
    删除数据
    :param request:
    :return:
    """

    NewCitizen.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
残疾人
"""


def handicapped(request):
    """
    残疾人模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Handicapped.objects.filter().all())]
        return render(request, 'handicapped.html', {"data_list": data_list})


def get_handicapped_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Handicapped.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_handicapped(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Handicapped.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_handicapped(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Handicapped.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_handicapped(request):
    """
    删除数据
    :param request:
    :return:
    """

    Handicapped.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
退役军人
"""


def veterans(request):
    """
    退役军人模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Veterans.objects.filter().all())]
        return render(request, 'veterans.html', {"data_list": data_list})


def get_veterans_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Veterans.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_veterans(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Veterans.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_veterans(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Veterans.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_veterans(request):
    """
    删除数据
    :param request:
    :return:
    """

    Veterans.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
低保
"""


def low_income(request):
    """
    低保模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(LowIncome.objects.filter().all())]
        return render(request, 'lowIncome.html', {"data_list": data_list})


def get_low_income_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(LowIncome.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_low_income(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        LowIncome.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_low_income(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        LowIncome.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_low_income(request):
    """
    删除数据
    :param request:
    :return:
    """

    LowIncome.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
低保边缘
"""


def low_income_margin(request):
    """
    低保边缘模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(LowIncomeMargin.objects.filter().all())]
        return render(request, 'lowIncomeMargin.html', {"data_list": data_list})


def get_low_income_margin_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(LowIncomeMargin.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_low_income_margin(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        LowIncomeMargin.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_low_income_margin(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        LowIncomeMargin.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_low_income_margin(request):
    """
    删除数据
    :param request:
    :return:
    """

    LowIncomeMargin.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
低收入
"""


def low_income_family(request):
    """
    低收入模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(LowIncomeFamily.objects.filter().all())]
        return render(request, 'lowIncomeFamily.html', {"data_list": data_list})


def get_low_income_family_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(LowIncomeFamily.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_low_income_family(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        LowIncomeFamily.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_low_income_family(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        LowIncomeFamily.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_low_income_family(request):
    """
    删除数据
    :param request:
    :return:
    """

    LowIncomeFamily.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
农业
"""


def agricultural(request):
    """
    农业模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Agricultural.objects.filter().all())]
        return render(request, 'agricultural.html', {"data_list": data_list})


def get_agricultural_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Agricultural.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_agricultural(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Agricultural.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_agricultural(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Agricultural.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_agricultural(request):
    """
    删除数据
    :param request:
    :return:
    """

    Agricultural.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))


"""
环境
"""


def environment(request):
    """
    环境模块
    :param request:
    :return:
    """
    if request.method == "GET":
        data_list = [model_to_dict(x) for x in list(Environment.objects.filter().all())]
        return render(request, 'environment.html', {"data_list": data_list})


def get_environment_by_id(request):
    """
    通过id获取数据
    :param request:
    :return:
    """
    data = model_to_dict(Environment.objects.filter(id=request.POST.get("id")).last())

    return HttpResponse(json.dumps(data))


def create_environment(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))
    try:
        Environment.objects.create(**data)
    except Exception as e:
        print(e)
    else:
        print('添加成功...')
    return HttpResponse(json.dumps({'msg': '添加成功...'}))


def update_environment(request):
    """
    创建或者更新数据
    :param request:
    :return:
    """
    data = dict(zip(list(request.POST.keys()), list(request.POST.values())))

    try:
        data.pop('id')
        Environment.objects.filter(id=request.POST.get('id')).update(**data)
    except Exception as e:
        print(e)
    else:
        print('修改成功...')

    return HttpResponse(json.dumps({}))


def delete_environment(request):
    """
    删除数据
    :param request:
    :return:
    """

    Environment.objects.filter(id=request.POST.get("id")).delete()
    return HttpResponse(json.dumps({'msg': '删除成功...'}))